local game = require "club.threecard.roulette"
local Vector3 = CS.UnityEngine.Vector3

local M = game.ui.element()

local no2angle = 
{
    [0] = -15,
    [32] = -5,
    [15] = 4.53,
    [19] = 14.3,
    [4] = 24.4,
    [21] = 34,
    [2] = 43.9,
    [25] = 53.2,
    [17] = 62.8,
    [34] = 72.3,
    [6] = 82.5,
    [27] = 91.9,
    [13] = 102.1,
    [36] = 111.7,
    [11] = 121.5,
    [30] = 131.3,
    [8] = 141.3,
    [23] = 150.9,
    [10] = 161,
    [5] = 170.4,
    [24] = 180.2,
    [16] = 190.1,
    [33] = 199.7,
    [1] = 209.5,
    [20] = 219.1,
    [14] = 228.6,
    [31] = 238.3,
    [9] = 247.8,
    [22] = 257.5,
    [18] = 266.8,
    [29] = 276.6,
    [7] = 286.2,
    [28] = 295.8,
    [12] = 306,
    [35] = 315.5,
    [3] = 325.2,
    [26] = 335.6
}

function M:ctor()
    self.res = "club.threecard.roulette/ui/ingame.b:result_shower"
end

function M:result(no)
    self.result_r:SetActive(false)
    self.result_g:SetActive(false)
    self.result_b:SetActive(false)

    self.result_txt.text = no
    local angle = no2angle[no]
    self.root.localEulerAngles = Vector3(0, 0, angle)

    self.animator:Play("result_show_clip")

    local bet_area_gen = game.require "ui.ingame.bet_area_gen"
    local is_big = false
    local is_red = false
    local is_odd = false
    if no == 0 then
        self.result_g:SetActive(true)
    else
        is_big = bet_area_gen.is_big_num({no})
        is_red = bet_area_gen.is_red_num({no})
        is_odd = bet_area_gen.is_odd_num({no})

        self.result_r:SetActive(is_red)
        self.result_b:SetActive(not is_red)
    end
    local time = 0.7
    self:start_coroutine(function()
        game.coroutine.wait(time)
        game.audio.post(string.format("roulette:%s", no))
        if no == 0 then
            return
        end

        game.coroutine.wait(time)
        if is_big then
            game.audio.post("roulette:bigger")
        else
            game.audio.post("roulette:smaller")
        end

        game.coroutine.wait(time)
        if is_red then
            game.audio.post("roulette:red")
        else
            game.audio.post("roulette:black")
        end

        game.coroutine.wait(time)
        if is_odd then
            game.audio.post("roulette:odd")
        else
            game.audio.post("roulette:even")
        end
    end)
end

function M:hide()
    if self.animator then
        self.animator:Play("result_hide_clip")
    end
end

return M